package com.wms.security.filter;

import com.wms.plugin.JwtPlugin;
import io.jsonwebtoken.Claims;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class UrlFilter {

    private UrlFilter nextFilter;

    public abstract void filter(HttpServletRequest request, HttpServletResponse response) throws Exception;

    public void setUrlFilter(UrlFilter urlFilter){
        this.nextFilter = urlFilter;
    }

    public void handler(HttpServletRequest request, HttpServletResponse response) throws Exception{
        this.filter(request, response);
        if (this.nextFilter != null){
            this.nextFilter.handler(request, response);
        }
    }

    public static Claims getClaims(HttpServletRequest request, JwtPlugin jwtPlugin) {
        String authorization = request.getHeader("Authorization");
        if (authorization == null || !authorization.startsWith("Bearer ")) {
            return null;
        }

        String token = authorization.split(" ")[1];
        return jwtPlugin.parseJWT(token);
    }


}
